<?php
	require_once("../process/session.php");
	$my_session = new session();
	$db = $my_session->getDB_torneig();

		print_r($_POST);
	if(dadesOk()){


		switch($_POST["function"]){
			/*********************	AFEGIR CATEGORIA  *********************/	
			case "afegir_cat":
				$query="Insert into categories values ('".$_POST["nom_cat"]."','".$_POST["min_part"]."','".$_POST["max_part"]."
														','".$_POST["sexe"]."','".$_POST["nivell"]."','".$_POST["desc_cat"]."')";

            	$res = $db->executar($query);

				if($res === FALSE)	echo "Categoria ja existeix!";
				break;
				
				
			/*********************	ELIMINAR CATEGORIA  *********************/	
			case "eliminar_cat";
				
				
				/*
				$query= "Select nom From equips Where categoria = '". $_POST["eliminar_cat"] ."'";

				$res = $db->consulta($query);
				
				if(sizeof($res)>0){
					$missatge = "Si elimina la categoria ".$_POST['eliminar_cat'].", eliminareu els seguents equips:\\n\\n";
					for ($i=0; $i < sizeof($res)-1; $i++) { 
						$missatge .= $res[$i]["nom"].", ";
					}
					$missatge .= $res[sizeof($res)-1]["nom"];
					$missatge .= "\\n\\nDesitja continuar?";
				} else {
					$missatge = "Eliminar la categoria ".$_POST['eliminar_cat']."?";
				}
				echo $missatge;
				
				$url = "../process/p_admin_torneig_db.php";
				*/
				
				//if(confirmacio($missatge, $url)){
								
							
					$delete = "Delete 
							   From	equips
							   Where categoria = '".$_POST['eliminar_cat']."'" ;	
									
					if(!$db->executar($delete))
						echo "ERROR DELETE";
					else
						echo "CORRECTE";
					
												
					$delete = "Delete 
							   From	categories
							   Where nom = '".$_POST['eliminar_cat']."'" ;	
									
					if(!$db->executar($delete))
						echo "ERROR DELETE";
					else
						echo "CORRECTE";
					
												
					$delete = "Delete 
							   From	cat_pista
							   Where categoria = '".$_POST['eliminar_cat']."'" ;	
									
					if(!$db->executar($delete))
						echo "ERROR DELETE";
					else
						echo "CORRECTE";
					// BORRAR EQUIPS I PARTICIPANTS
				//}
				break;
				
			/*********************	AFEGIR PISTA  *********************/	
			case "afegir_pista";
				$cats = array();
				foreach ($_POST as $key => $value) {
					if($key != "function" && $key != "nom_pista")	$cats[] = $value;
				}
				
				$insert = "Insert Into pistes (nom) Values ('". $_POST['nom_pista'] ."')";
				$db->executar($insert);	
				for ($i=0; $i < sizeof($cats); $i++) { 

					$insert = "Insert Into cat_pista (nom, categoria) Values ('". $_POST['nom_pista'] ."', '". $cats[$i] ."')";
					$db->executar($insert);	
				}

				$consult = "Select count(*) From pistes";
				$res = $db->consulta($consult);
				
				$update = "Update info Set n_pistes = ". $res[0][0];
				$db->executar($update);
				

				break;
			/*********************	ELIMINAR PISTA  *********************/	
			case "eliminar_pista";
				$delete = "Delete From pistes Where nom = '". $_POST['eliminar_pista'] . "'";
				$db->executar($delete);
				$delete = "Delete From cat_pista Where nom = '". $_POST['eliminar_pista'] . "'";
				$db->executar($delete);
				
				$consult = "Select count(*) From pistes";
				$res = $db->consulta($consult);
				
				$update = "Update info Set n_pistes = ". $res[0][0];
				$db->executar($update);

				break;	
			/*********************	HORES  *********************/	
			case "hores";
				$trunc = "Truncate Table hores_dies";
				$db->executar($trunc);
			
				$update = "Update info Set n_dies = '". $_POST['n_dies'] ."'";
				$db->executar($update);
				
				$n = $_POST["n_dies"];
				for ($i=1; $i <= $n; $i++) { 
					$insert = "Insert Into hores_dies(dia, h_ini, h_fi) Values ('". $i . "','". $_POST['sel_ini_'.$i] ."','". $_POST['sel_fi_'.$i] . "')";
					$db->executar($insert);
				}
				
				break;
			/*********************	ALTRES  *********************/	
			case "altres";
				if($_POST['pago'] == 'no'){
					$update = "Update info Set pagament = '" . 'n' . "'";
					$db->executar($update);
				} else if($_POST['pago'] == 'si'){
					$update = "Update info Set pagament = '" . 's' . "'";
					$db->executar($update);
				}
				
				if($_POST['restr'] == 'no'){
					$update = "Update info Set restr = '" . 'no' . "'";
					$db->executar($update);
				} else if($_POST['restr'] == 'una'){
					$update = "Update info Set restr = '" . 'una' . "'";
					$db->executar($update);
				} else if ($_POST['restr'] == 'una_x_2dies'){
					$update = "Update info Set restr = '" . 'una_x_2dies' . "'";
					$db->executar($update);
				} 
				break;
			default:
		}
	} 

	header("location: ../forms/f_admin_torneig.php");
	
	
	function dadesOk(){
		if(!isset($_POST["function"]))	return false;
		
		switch($_POST["function"]){
			case "afegir_cat":
				if(!isset($_POST["nom_cat"]) || $_POST["nom_cat"] == "")	return false;
				if(!isset($_POST["desc_cat"]))	return false;
				if(!isset($_POST["sexe"]) || !($_POST["sexe"] == "masculi" || $_POST["sexe"] == "femeni" || $_POST["sexe"] == "mixte"))	return false;
				if(!isset($_POST["nivell"]) || !($_POST["nivell"] == "infantil" || $_POST["nivell"] == "costellada" || 
												$_POST["nivell"] == "obert" || $_POST["nivell"] == "elit"))		return false;
				if(!isset($_POST["min_part"]) || !is_numeric($_POST["min_part"]))	return false;
				if(!isset($_POST["max_part"]) || !is_numeric($_POST["max_part"]))	return false;
				if(($_POST["min_part"]*2)-1 > $_POST["max_part"])				return false;
				break;
			case "eliminar_cat";
				if(!isset($_POST["eliminar_cat"]) || $_POST["eliminar_cat"] == "")	return false;
				break;
			case "afegir_pista";
				if(!isset($_POST["nom_pista"]) || $_POST["nom_pista"] == "")	return false;
				break;
			case "eliminar_pista";
				if(!isset($_POST["eliminar_pista"]) || $_POST["eliminar_pista"] == "")	return false;
				break;	
			case "hores";
				if(!isset($_POST['n_dies']) || $_POST['n_dies'] == "" || !is_numeric($_POST['n_dies']))		return false;
				$n = $_POST["n_dies"];
				for ($i=1; $i <= $n; $i++) { 
					if(!is_numeric($_POST['sel_ini_'.$i]) || !is_numeric($_POST['sel_fi_'.$i]))	return false;
					if($_POST['sel_ini_'.$i] > 24 || $_POST['sel_ini_'.$i] < 8 || $_POST['sel_fi_'.$i] > 24 || $_POST['sel_fi_'.$i] < 8 )	return false;
					if($_POST['sel_ini_'.$i] >= $_POST['sel_fi_'.$i])	return false;
				}
				break;
			case "altres";
				if(!isset($_POST['restr']) || !isset($_POST['pago']))	return false;
				break;
			default:
		}
		return true;
		
	}
	
	function confirmacio($missatge, $url){
		
		?>		

			<script language="Javascript"> 

			var url = "<?php echo $url?>";
			var txt = "<?php echo $missatge?>";
			confirmar=confirm(txt); 
			if (confirmar){ 
				window.location = url;
			}
			</script>			
		<?php
	}

	
	
	
	
	
	
	
?>